package com.azt.front.action.msg;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.azt.api.pojo.Dictionary;
import com.azt.api.pojo.MsgConfig;
import com.azt.api.pojo.MsgEnquirySend;
import com.azt.api.pojo.MsgEnquiryUnsendVO;
import com.azt.api.pojo.MsgUser;
import com.azt.api.pojo.MsgUserUnsend;
import com.azt.api.service.MsgConfigService;
import com.azt.api.service.MsgUserService;
import com.azt.front.permission.RequireCompany;
import com.azt.front.permission.RequirePermission;
import com.azt.front.permission.RequireUser;
import com.azt.front.utils.ContextUtil;
import com.azt.model.AjaxJson;
import com.azt.model.page.Pagination;
import com.azt.utils.CommonUtil;
import com.azt.utils.RequestUtils;

@Controller
@RequireUser
@RequestMapping("/msg")
public class MsgAction {
	 
	 @Autowired
	 private MsgConfigService msgConfigService;
	 
	 @Autowired
	 private MsgUserService msgUserService;
	 
	 
	 private void fillInNoReadMsgCount(Model model){
		 model.addAttribute("allmsg", msgUserService.selectNoReadMsgCountByUserid(ContextUtil.getUser().getId()));
	 }
	 
	 @RequestMapping("/msglist")
	 public String MsgList(HttpServletRequest request, Integer pageNo,  HttpServletResponse response, ModelMap model){
	 	Map<String, String> searchMap = new HashMap<String,String>();
		String thisuserId=ContextUtil.getUser().getId().toString();
		String code=request.getParameter("code");
		String ifRead=request.getParameter("ifRead");
		searchMap.put("userId", thisuserId);
		searchMap.put("msgType", "msgType");
		searchMap.put("code", code);
		searchMap.put("ifRead", ifRead);
		Pagination pagination = msgUserService.selectMsgUserPage(pageNo == null ? 1 : pageNo,12, searchMap);
		pagination.getParams().put("code", code);
		pagination.getParams().put("ifRead", ifRead);
		model.put("pagination", pagination);
		model.put("notread", msgUserService.selectMsgCountNotRead(searchMap));//当前code未读数
		//model.put("allmsg", msgUserService.selectNoReadMsgCountByUserid(ContextUtil.getUser().getId()));//所有未读R
		//RequestUtils.MapToModelMap(searchMap, request);
		return "demo/mesage/1站内信";
	 }
	 
	 @RequestMapping("/changeMsgReaded")
	 @ResponseBody
	 public AjaxJson changeMsgReaded(MsgUser msg){
		 AjaxJson aj=new AjaxJson();
		 try {
			 msg.setIfRead(1);
			 msgUserService.changeMsgReaded(msg);
			 aj=AjaxJson.success("success");
		 } catch (Exception e) {
			 e.printStackTrace();
			 aj=AjaxJson.error("系统繁忙");
		 }
		 return aj;
	 }
	 
	 @RequestMapping("/changeAlltoRead")
	 @ResponseBody
	 public AjaxJson changeAlltoReaded(String chooseids){
		 AjaxJson aj=new AjaxJson();
		 try {
			String[] idsarr=chooseids.split(",");
			msgUserService.changeAlltoReaded(idsarr);
		 } catch (Exception e) {
			e.printStackTrace();
			aj=AjaxJson.error("系统繁忙");
		 }
		 return aj;
	 }
	 
	 @RequestMapping("/delchooseMsg")
	 @ResponseBody
	 public AjaxJson delchooseMsg(String chooseids){
		 AjaxJson aj=new AjaxJson();
		 try {
			 String[] idsarr=chooseids.split(",");
			 msgUserService.delchooseMsg(idsarr);
		 } catch (Exception e) {
			 e.printStackTrace();
			 aj=AjaxJson.error("系统繁忙");
		 }
		 return aj;
	 }
	 

	 @RequireCompany
	 @RequestMapping("/msgCon")
	 public String msgConfigList(Model model){
		 Integer roletype=ContextUtil.getCurrentRoleType();
		 Integer userId=ContextUtil.getUserId();
		 if(roletype==null||userId==null){
			 return "demo/mesage/出错了";
		 }
		 List<MsgConfig> msgConfigList=msgUserService.selectMainMsgConfig(userId,roletype);
		 List<Dictionary> msgTypes=msgUserService.selectMsgTypeDic();
		 Integer roleType= ContextUtil.getCurrentRoleType();
		 for (Dictionary d:msgTypes) {
			String[] des=d.getDescr().split("-");
			d.setDescr(roleType.equals(1)?des[0]:des[1]);
		 }
		 model.addAttribute("msgConfigList", msgConfigList);
		 model.addAttribute("msgTypes", msgTypes);
		 return "demo/mesage/2消息接收管理-采购商";
	 }
	 
	
	 @RequireCompany
	 @ResponseBody
	 @RequestMapping("/clickCheckBox")
	 public AjaxJson clickCheckBox(String code){
		 AjaxJson aj=new AjaxJson();
		 Integer userid=ContextUtil.getUserId();
		 String[] param=code.split("-");
		 try {
			 MsgUserUnsend unSend=msgUserService.getMagUserUnsendByUidAndCode(userid, param[1]);
			 if(unSend==null){
				 MsgUserUnsend send=new MsgUserUnsend(1,1,1);
				 send.setUserId(userid);
				 send.setMsgCode(param[1]);
				 if("Mail".equals(param[0])){
					 send.setIfMail(0);
				 }else if("Email".equals(param[0])){
					 send.setIfEmail(0);
				 }else{
					 send.setIfMobile(0);
				 }
				 //添加
				 send.setCreatetime(new Date());
				 msgUserService.addNewMsgUserUnsend(send);
		
			 }else{
				 if("Mail".equals(param[0])){
					 unSend.setIfMail(unSend.getIfMail().equals(1)?0:1);
				 }else if("Email".equals(param[0])){
					 unSend.setIfEmail(unSend.getIfEmail().equals(1)?0:1);
				 }else{
					 unSend.setIfMobile(unSend.getIfMobile().equals(1)?0:1);
				 }
				 if(unSend.getIfEmail().equals(1)&&unSend.getIfMail().equals(1)&&unSend.getIfMobile().equals(1)){
					 //删除
					 msgUserService.deleteMsgUserUnsend(unSend.getId());
				 }else{
					 //修改
					 msgUserService.updateMsgUserUnsend(unSend);
				 }
			 }
		 } catch (Exception e) {
			e.printStackTrace();
			aj=AjaxJson.error("系统繁忙");
		 }
		 return aj;
	 }
	 
	
	 @RequireCompany
	 @RequestMapping("/msgEnquiryCon")
	 public String showEnquiryCon(Model model){
		 Integer userId=ContextUtil.getUserId();
		 List<MsgEnquiryUnsendVO> enquiryUnsendList=msgUserService.selectMainMsgEnquiry(userId);
		 model.addAttribute("enlist", enquiryUnsendList);
		 return "demo/mesage/4推送询价消息类型";
	 }
	 
	 @RequestMapping("/clickEnqCheckBox")
	 @ResponseBody
	 public AjaxJson clickEnqCheckBox(String type,Integer eid,Integer cid){
		 AjaxJson aj=new AjaxJson();
		 Integer userId=ContextUtil.getUserId();
		 try {
			 MsgEnquirySend enqu=msgUserService.selectEnqUnsendByUidAndCid(userId, cid);
			 if(enqu==null){
				 //添加
				 MsgEnquirySend msgEnquiry=new MsgEnquirySend();
				 if("Design".equals(type)){
					 msgEnquiry.setIfDesign(1);
					 msgEnquiry.setIfEnquiry(0);
				 }else{
					 msgEnquiry.setIfEnquiry(1);
					 msgEnquiry.setIfDesign(0);
				 }
				 msgEnquiry.setUserId(userId);
				 msgEnquiry.setCreatetime(new Date());
				 msgEnquiry.setUpdatetime(new Date());
				 msgEnquiry.setCategoryId(cid);
				 msgUserService.addMsgEnquiryUnsend(msgEnquiry);
			 }else{
				 if("Design".equals(type)){
					 enqu.setIfDesign(enqu.getIfDesign().equals(1)?0:1);
				 }else{
					 enqu.setIfEnquiry(enqu.getIfEnquiry().equals(1)?0:1);
				 }
				 if(enqu.getIfDesign().equals(0)&&enqu.getIfEnquiry().equals(0)){
					 //删除
					  msgUserService.deleteEnqUnsendByUidAndCid(enqu.getId());
				 }else{
					 //修改
					 msgUserService.updateEnqUnsend(enqu);
				 }
			 }
		 } catch (Exception e) {
			e.printStackTrace();
			aj=AjaxJson.error("系统繁忙");
		 }
		 return aj;
	 }
}
